using System;
using System.IO;

public class PathUtils
{
  public static bool Valid(string path)
  {
    try
    {
      // 尝试获取路径的全路径
      string fullPath = Path.GetFullPath(path);
      return true;
    }
    catch (ArgumentException)
    {
      // 路径无效
      return false;
    }
    catch (PathTooLongException)
    {
      // 路径太长
      return false;
    }
    catch (NotSupportedException)
    {
      // 路径包含非法字符
      return false;
    }
  }

  public static bool InValid(string path)
  {
    return !Valid(path);
  }

  public static string GetDirPath(string path)
  {
    if (Directory.Exists(path))
    {
      return path;
    }
    else if (File.Exists(path))
    {
      return Path.GetDirectoryName(path);
    }
    else
    {
      return null;
    }
  }

  public static string GetDirName(string path)
  {
    if (Directory.Exists(path))
    {
      return Path.GetFileName(path);
    }
    else if (File.Exists(path))
    {
      return Path.GetFileName(Path.GetDirectoryName(path));
    }
    else
    {
      return null;
    }

  }
}

